package com.xinzhi.service.impl;

import com.xinzhi.dao.InsertDao;
import com.xinzhi.dao.LoginUserDao;
import com.xinzhi.pojo.BankCard;
import com.xinzhi.service.IInsertService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import com.xinzhi.utils.TengXunSMSUtils; // 导入TengXunSMSUtils类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class InsertServiceImpl implements IInsertService {
    @Autowired
    private InsertDao insertDao;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private LoginUserDao loginUserDao;

    @Autowired
    private TengXunSMSUtils tengXunSMSUtils; // 注入TengXunSMSUtils实例
    @Override
    public Result addPhone(String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        if (!phone.matches("^1[3456789]\\d{9}$")) {
            return Result.error("手机号格式不正确");
        }
        // 去重
        if (loginUserDao.login(phone) != null) {
            return Result.error("该手机号已注册");
        }
        LocalDateTime registrationTime = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        int result = insertDao.addPhone(phone,registrationTime ,localDateTime);
        if (result > 0) {
            return Result.ok("注册成功");
        } else {
            return Result.error("注册失败");
        }
    }
    @Override
    public Result sendVerificationCode(String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return Result.error("手机号不能为空");
        }
        // 生成验证码
        String code = TengXunSMSUtils.generateValidateCode(6).toString();
        // 将验证码存入Redis，设置过期时间为5分钟（300秒）
        boolean setResult = redisUtils.set(phone, code, 300);
        if (!setResult) {
            return Result.error("验证码发送失败，请重试");
        }
        // 调用腾讯工具类发送验证码
        try {
            String templateID = "2194054"; // 替换为您的模板ID
            tengXunSMSUtils.sendShortMessage(templateID, phone, code, "5");
            return Result.ok("验证码已发送，请注意查收");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发送验证码失败：" + e.getMessage());
        }
    }
  
    public Result verifyCode(String phone, String code) {
        if (phone == null || phone.trim().isEmpty() || code == null || code.trim().isEmpty()) {
            return Result.error("手机号或验证码不能为空");
        }
        String cachedCode = (String) redisUtils.get(phone);
        if (cachedCode != null && cachedCode.equals(code)) {
            // 验证码正确，执行相关业务逻辑，例如完成注册等
            // 这里简化为直接返回成功
            return Result.ok("验证码验证成功，可以进行下一步操作");
        } else {
            return Result.error("验证码不正确或已过期，请重新获取验证码");
        }
    }

    @Override
    public List<BankCard> queryBankCard(String phone) {
        List<BankCard> bankCards = new ArrayList<>();
        try {
            bankCards = insertDao.selectBankCard(phone);
            if (bankCards == null || bankCards.isEmpty()) {
                // 如果找不到任何银行卡信息，可以选择记录日志或抛出异常
                System.out.println("No bank cards found for phone number: " + phone);
            }
        } catch (Exception e) {
            // 处理可能的异常
            System.err.println("Error querying bank cards for phone number: " + phone);
            e.printStackTrace();
        }
        return bankCards;
    }

    @Override
    public Result deleteUser(String phone) {
        insertDao.deletePhone(phone);
        return Result.ok("删除成功");





    }


}
